﻿namespace GSC.WebControls
{
    using System;
    using System.Collections.Generic;
    /// <summary>
    /// Post字符处理。
    /// </summary>
    /// <remarks>Post字符处理。</remarks>
    public static class PostDataStringHelper
    {
        public static string ArrayToString(params string[] list)
        {
            if (list.Length <= 0)
            {
                return string.Empty;
            }
            string strLengthLine = String.Empty;
            string strStringLine = String.Empty;
            for (int i = 0; i < list.Length; i++)
            {
                strLengthLine += ":" + list[i].Length;
                strStringLine += list[i];
            }
            return strStringLine + '$' + strLengthLine.Substring(1);
        }
        public static string ArrayToString(params object[] list)
        {
            string[] strList = new string[list.Length];
            for (int i = 0; i < list.Length; ++i)
            {
                if (list[i] is string)
                {
                    strList[i] = (string)list[i];
                }
                else
                {
                    strList[i] = Convert.ToString(list[i]);
                }
            }
            return ArrayToString(strList);
        }
        public static string[] StringToArray(string strStringLine)
        {
            int index = strStringLine.LastIndexOf('$');
            if (index == -1)
            {
                return new string[] { strStringLine };
            }
            string strLengthLine = strStringLine.Substring(index + 1);
            string[] aryLength = strLengthLine.Split(new char[] { ':' });
            string[] list = new string[aryLength.Length];
            int start = 0;
            for (int i = 0; i < list.Length; ++i)
            {
                int length = int.Parse(aryLength[i]);
                list[i] = strStringLine.Substring(start, length);
                start += length;
            }
            return list;
        } 
    }
}